package com.lagou.stage04;

public class TicketStore {
    private int ticketNum = 0;

    public TicketStore() {
    }

    public TicketStore(int ticketNum) {
        this.ticketNum = ticketNum;
    }

    public synchronized void buyTicket() {
        notify();
        if (0 < ticketNum) {
            System.out.println("购买成功！当前余票：" + ticketNum);
            ticketNum--;
        } else {
            System.out.println("出票失败，当前无票。");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void addTicketNum(int addTicketNum) {
        notify();
        if (1 > ticketNum) {
            System.out.println("添加" + addTicketNum + "张票。");
            this.ticketNum += addTicketNum;
        } else {
            System.out.println("当前余票" + ticketNum + "，不需要添加票。");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public int getTicketNum() {
        return ticketNum;
    }
}
